﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Data.AID;
using System.Collections.Generic;
using Business.AID;
using Business.AID.Extensions;
using Microsoft.SharePoint;

namespace Presentation.AID.WebPart.Presentation.AID.VWPConsultarMetaAuditoria
{
    public partial class VWPConsultarMetaAuditoriaUserControl : UserControl
    {
        private List<Formulario> ListaFormulario
        {
            get { return Cache["ListFormulario"] as List<Formulario>; }
            set { Cache["ListFormulario"] = value; }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ColaboradorBO colaboradorBO = new ColaboradorBO();
            Colaborador colaborador = colaboradorBO.GetEmployeeByLogin(SPContext.Current.Web.CurrentUser.LoginName);
            if (colaborador != null && colaborador.Funcao.Contains("Coordenador de Auditoria"))
            {
                btnAIDValidacao.Visible = true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetGridView();
            }
        }

        #region Métodos Privados

        private void SetGridView()
        {
            ListaFormulario = GetAuditForms().ToList();
            gdvFormulariosAuditoria.DataSource = ListaFormulario;
            gdvFormulariosAuditoria.DataBind();
        }
        private void FilterReport()
        {
            string _nome = txtNome.Text.RemoveAccents();
            string _matricula = txtMatricula.Text;

            if (ListaFormulario != null)
            {
                List<Formulario> Lista = ListaFormulario;

                if (_nome != "" || _matricula != "")
                {
                    if (_nome != "")
                    {
                        Lista = Lista.Where(p => p.Nome.ToLower().RemoveAccents().Contains(_nome.ToLower())).ToList();
                    }

                    if (_matricula != "")
                    {
                        Lista = Lista.Where(p => p.Matricula.ToLower().Contains(_matricula.ToLower())).ToList();
                    }
                }
                gdvFormulariosAuditoria.DataSource = Lista;
                gdvFormulariosAuditoria.DataBind();
            }
            else
            {
                gdvFormulariosAuditoria.DataSource = ListaFormulario;
                gdvFormulariosAuditoria.DataBind();
            }
        }
        private List<Formulario> GetAuditForms()
        {
            FormularioBO formularioBO = new FormularioBO();
            return formularioBO.GetAuditForms().OrderBy(p => p.AprovacaoAuditoria).ToList();
        }

        #endregion
        
        #region Botões

        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            FilterReport();
        }
        protected void btnLimpar_Click(object sender, EventArgs e)
        {
            txtNome.Text = string.Empty;
            txtMatricula.Text = string.Empty;
            SetGridView();
        }
        protected void btnAnexar_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            GridViewRow row = (GridViewRow)button.NamingContainer;
            int rowIndex = row.RowIndex;
            DataKey dtKey = gdvFormulariosAuditoria.DataKeys[rowIndex];

            int idForm = Convert.ToInt32(dtKey["ID"]);

            FormularioBO formularioBO = new FormularioBO();
            Formulario formulario = formularioBO.GetFormById(idForm);

            string AttachmentUrl = formulario.AttachmentUrl.Trim();
            string AttachmentName = formulario.AttachmentName;

            if (formulario.AttachmentName != "")
            {
                System.Web.HttpContext.Current.Session["FileName"] = AttachmentName;
                System.Web.HttpContext.Current.Session["Attachment"] = AttachmentUrl;
            }
            else
            {
                Response.Write(@"<script> alert('Anexo não foi encontrado em nosso sistema.'); </script>");
            }

            if (AttachmentUrl != string.Empty || AttachmentUrl != "")
            {
                Response.Redirect("download.aspx");
            }
        }
        
        #endregion

        #region Eventos
        
        protected void gdvFormulariosGestor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvFormulariosAuditoria.DataSource = ListaFormulario;
            gdvFormulariosAuditoria.PageIndex = e.NewPageIndex;
            gdvFormulariosAuditoria.DataBind();
        }

        #endregion

        protected void btnAIDValidacao_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"/SitePages/RelatorioValidacao.aspx");
        }
    }
}
